package com.darkplanet.management.dao;

import com.darkplanet.management.domain.Role;
import com.darkplanet.management.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;
import java.util.List;

public class RoleDaoimpl implements IRoleDao {

    QueryRunner queryRunner=new QueryRunner(JDBCUtil.getDs());
    @Override
    public Role getRoleId(Integer id) {
       Role role=null;
        String sql="select * from role where role_id=?";
           try {
               role=queryRunner.query(sql, new BeanHandler<Role>(Role.class), id);
           }catch(SQLException e){
               e.printStackTrace();
           }
        return role;
    }

    @Override
    public List<Role> getRoles()  {
      List <Role> roles=null;
      String sql="select * from role";
      try {
          roles = queryRunner.query(sql, new BeanListHandler<Role>(Role.class));
      }catch(SQLException e){
          e.printStackTrace();
      }
        return roles;
    }

    @Override
    public void addRole(String type) {
        String sql="insert into role (type) values(?)";
        try {
            queryRunner.update(sql,type);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void deleteRole(Integer id) {
           String sql="delete  from role where role_id=?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

//    @Override
//    public Role queryRole(Integer id) {
//        Role role=null;
//        String sql="select * from role where role_id=?";
//        try {
//            role=queryRunner.query(sql,new BeanHandler<>(Role.class),id);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return role;
//    }

    @Override
    public List queryAllRole() {
        List<Role> role=null;
        String sql="select * from role";
        try {
            role=queryRunner.query(sql,new BeanListHandler<>(Role.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return role;
    }
}
